# -*- coding: utf-8 -*-
import scrapy
import time
import re
from scrapy.http import Request
import requests
import random
import json

class LianjiaSpider(scrapy.Spider):
    name = 'lianjia'
    allowed_domains = ['lianjia.com']
    start_urls = ['https://bj.lianjia.com/fangjia/']

    def parse(self, response):
        url = requests.get(response.url)
        # print(response.url)
        # print(url.text)
        # 'https://bj.lianjia.com/fangjia/priceMap/chaoyang/'
        # pattern = r'/fangjia/(.*?)/'
        # s = re.search(pattern, response.url)
        # pos = s.group(1)
        ur = 'https://bj.lianjia.com/fangjia/priceMap/'
        ur = requests.get(ur)
        conts = json.loads(ur.text)
        for i in range(2, 100):
            try:
                url = 'https://bj.lianjia.com/fangjia/priceTrend/?region=district&region_id=' + str(conts[str(i)]['id'])
                urls = requests.get(url)
                cont = json.loads(urls.text)
                if len(cont) == 0:
                    # print('备用')
                    url = 'https://bj.lianjia.com/fangjia/priceTrend/?region=bizcircle&region_id=' + str(conts[str(i)]['id'])
                    urls = requests.get(url)
                    cont = json.loads(urls.text)
                dict = {'月份': cont['currentLevel']['month'], '地区': conts[str(i)]['name'],
                        '全部挂牌均价': cont['currentLevel']['listPrice']['total'],
                        '一居挂牌均价': cont['currentLevel']['listPrice']['1_bed'],
                        '二居挂牌均价': cont['currentLevel']['listPrice']['2_bed'],
                        '三居挂牌均价': cont['currentLevel']['listPrice']['3_bed'],
                        '其他挂牌均价': cont['currentLevel']['listPrice']['other'],
                        '全部参考均价': cont['currentLevel']['dealPrice']['total'],
                        '一居参考均价': cont['currentLevel']['dealPrice']['1_bed'],
                        '二居参考均价': cont['currentLevel']['dealPrice']['2_bed'],
                        '三居参考均价': cont['currentLevel']['dealPrice']['3_bed'],
                        '其他参考均价': cont['currentLevel']['dealPrice']['other'], '经度': conts[str(i)]['longitude'], '纬度': conts[str(i)]['latitude']}
                url = url + '&analysis=1'
                url = requests.get(url)
                cont = json.loads(url.text)
                # 'https://bj.lianjia.com/fangjia/priceTrend/?region=district&region_id=23008613&analysis=1'
                dict['新增房源量'] = cont['houseAmount']
                dict['带看次数'] = cont['showAmount']
                yield dict
            except:
                print('error')
    # def parse_info(self, response):
    #     urls = requests.get(response.url)
    #     cont = json.loads(urls.text)
    #     if len(cont) == 0:
    #         print('备用')
    #         print(self.id)
    #         url = 'https://bj.lianjia.com/fangjia/priceTrend/?region=bizcircle&region_id=' + str(self.id)
    #         urls = requests.get(url)
    #         cont = json.loads(urls.text)
    #     # print(cont)
    #     dict = {'月份': cont['currentLevel']['month'], '地区': self.area,
    #             '全部挂牌均价': cont['currentLevel']['listPrice']['total'],
    #             '一居挂牌均价': cont['currentLevel']['listPrice']['1_bed'],
    #             '二居挂牌均价': cont['currentLevel']['listPrice']['2_bed'],
    #             '三居挂牌均价': cont['currentLevel']['listPrice']['3_bed'],
    #             '其他挂牌均价': cont['currentLevel']['listPrice']['other'],
    #             '全部参考均价': cont['currentLevel']['dealPrice']['total'],
    #             '一居参考均价': cont['currentLevel']['dealPrice']['1_bed'],
    #             '二居参考均价': cont['currentLevel']['dealPrice']['2_bed'],
    #             '三居参考均价': cont['currentLevel']['dealPrice']['3_bed'],
    #             '其他参考均价': cont['currentLevel']['dealPrice']['other'], '经度': self.longitude, '纬度': self.latitude}
    #     url = url + '&analysis=1'
    #     url = requests.get(url)
    #     cont = json.loads(url.text)
    #     # 'https://bj.lianjia.com/fangjia/priceTrend/?region=district&region_id=23008613&analysis=1'
    #     dict['新增房源量'] = cont['houseAmount']
    #     dict['带看次数'] = cont['showAmount']
    #     yield dict
    #     # item = {'titles': response.xpath('/html/body/div[3]/div/div/div[1]/h1/text()').getall(),
    #     #         '房屋户型': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[1]/text()').getall(),
    #     #         '建筑面积': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[3]/text()').getall(),
    #     #         '所在楼层': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[2]/text()').getall(),
    #     #         '室内面积': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[5]/text()').getall(),
    #     #         '房屋朝向': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[7]/text()').getall(),
    #     #         '装修情况': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[9]/text()').getall(),
    #     #         '供暖方式': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[11]/text()').getall(),
    #     #         '户型结构': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[4]/text()').getall(),
    #     #         '建筑结构': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[8]/text()').getall(),
    #     #         '建筑类型': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[6]/text()').getall(),
    #     #         '梯户比例': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[10]/text()').getall(),
    #     #         '配备电梯': response.xpath('//*[@id="introduction"]/div/div/div[1]/div[2]/ul/li[12]/text()').getall(),
    #     #         '挂牌时间': response.xpath('//*[@id="introduction"]/div/div/div[2]/div[2]/ul/li[1]/span[2]/text()').getall(),
    #     #         '房屋年限': response.xpath('//*[@id="introduction"]/div/div/div[2]/div[2]/ul/li[5]/span[2]/text()').getall(),
    #     #         '抵押信息': response.xpath('//*[@id="introduction"]/div/div/div[2]/div[2]/ul/li[7]/span[2]/text()').getall(),
    #     #         '交易权属': response.xpath('//*[@id="introduction"]/div/div/div[2]/div[2]/ul/li[2]/span[2]/text()').getall(),
    #     #         '房屋用途': response.xpath('//*[@id="introduction"]/div/div/div[2]/div[2]/ul/li[4]/span[2]/text()').getall(),
    #     #         '产权所属': response.xpath('//*[@id="introduction"]/div/div/div[2]/div[2]/ul/li[6]/span[1]/text()').getall(),
    #     #         '房屋备件': response.xpath('//*[@id="introduction"]/div/div/div[2]/div[2]/ul/li[8]/span[2]/text()').getall(),
    #     #         '总价格': response.xpath('/html/body/div[5]/div[2]/div[3]/span[1]/text()').getall(),
    #     #         '单位价格': response.xpath('/html/body/div[5]/div[2]/div[3]/div[1]/div[1]/span/text()').getall()}
    #     # yield item


